package com.bysj.money_diary;

import java.util.ArrayList;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

@SuppressLint({ "ViewHolder", "InflateParams" })
public class ChazhangbenAdapter extends BaseAdapter{	
	public static ArrayList<?> mingXiList;
	private Context context;
	private int flag = 0;
	
	Data dataHelper ;
	
	public ChazhangbenAdapter(Context context) {
		this.context = context;
		dataHelper= new Data(context);
	}
	
	public double[] getList(int year,int month,int day,int flag){
	    this.flag = flag;
	    double countZhiChu = 0,countShouRu = 0;
	    ArrayList<Zhichu> zhiChuList = new ArrayList<Zhichu>();
	    String selectionzhichu = Zhichu.ZC_YEAR+"="+year+" and "+Zhichu.ZC_MONTH+"="+month;
	    zhiChuList =  dataHelper.GetZhiChuList(selectionzhichu);
    	for(Zhichu zhichu:zhiChuList){
    		countZhiChu += zhichu.getZc_Count();
    	}
    	ArrayList<Shouru> shouRuList = new ArrayList<Shouru>();
        String selectionshouru = Shouru.SR_YEAR+"="+year+" and "+Shouru.SR_MONTH+"="+month;
        shouRuList =  dataHelper.GetShouRuList(selectionshouru);
        for(Shouru shouru:shouRuList){
        	countShouRu += shouru.getSr_Count();
    	}
	    if(flag==Chazhangben.zhichu_flag){
	    	mingXiList = zhiChuList;
	    }else if(flag==Chazhangben.shouru_flag){
            mingXiList = shouRuList;
	    }
	    return new double[]{countZhiChu,countShouRu};
	}

	@Override
	public int getCount() {
		return mingXiList.size();
	}

	@Override
	public Object getItem(int position) {
		return mingXiList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		convertView = LayoutInflater.from(context).inflate(R.layout.chazhangben_item, null);
		Zhichu zhichu = new Zhichu();
		Shouru shouru = new Shouru();
		
        TextView lb_text = (TextView) convertView.findViewById(R.id.chazhangben_leibie_item_text);
        TextView jine_text = (TextView) convertView.findViewById(R.id.chazhangben_jine_item_text);
        TextView beizhu_text = (TextView) convertView.findViewById(R.id.chazhangben_beizhu_item_text);
        TextView time_text = (TextView) convertView.findViewById(R.id.chazhangben_time_item_text);
		if(flag==Chazhangben.zhichu_flag){
		    zhichu = (Zhichu)mingXiList.get(position);
		    convertView.setTag(zhichu);
		    if(zhichu.getZc_SubItem().length()>0){
		        lb_text.setText(zhichu.getZc_Item()+">"+zhichu.getZc_SubItem());
		    }else{
		        lb_text.setText(zhichu.getZc_Item());
		    }
		    jine_text.setText(zhichu.getZc_Count()+"");
		    beizhu_text.setText(zhichu.getZc_Beizhu());
		    time_text.setText(zhichu.getZc_Day()+"日 "+zhichu.getZc_Time());
		}else if(flag==Chazhangben.shouru_flag){
		    shouru = (Shouru)mingXiList.get(position);
		    convertView.setTag(shouru);
		    lb_text.setText(shouru.getSr_Item());
		    jine_text.setText(shouru.getSr_Count()+"");
            beizhu_text.setText(shouru.getSr_Beizhu());
            time_text.setText(shouru.getSr_Day()+"日 "+shouru.getSr_Time());
		}
	return convertView;
	}

}
